<?php

App::uses('AppController', 'Controller');
App::import('Model', 'Image');
App::import('Model', 'Video');
App::import('Controller', 'Images');
App::import('Controller', 'Videos');

/**
 * Champions Controller
 *
 * @property Champion $Champion
 */
class ChampionsController extends AppController {
    
    var $Images;
    var $Videos;
    
    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->Champion->recursive = 0;
        $Image=new Image();
        $images=$Image->find('list', array('fields' => array('Image.id', 'Image.file_name')));
        $champions=$this->paginate();
        foreach($champions as $indice => $chp)
        {
            $aux=array('file_name' => $images[$chp['Champion']['id_image']]);
            array_push($champions[$indice]['Champion'],$aux);
        }
        $this->set('champions', $champions);
    }

    /**
     * view method
     *
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        $this->Champion->id = $id;
        if (!$this->Champion->exists()) {
            throw new NotFoundException(__('Invalid champion'));
        }
        $this->Champion->recursive = 0;
        $Image=new Image();
        $Video=new Video();
        $images=$Image->find('list', array('fields' => array('Image.id', 'Image.file_name')));
        $videos=$Video->find('list', array('fields' => array('Video.id', 'Video.url')));
        $champions=$this->Champion->findById($id);
        $auxImage=array('file_name' => $images[$champions['Champion']['id_image']]);
        $auxVideo=array('url' => $videos[$champions['Champion']['id_video']]);
        array_push($champions['Champion'],$auxImage);
        array_push($champions['Champion'],$auxVideo);
        $this->set('champions', $champions);
        return $champions;
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {
            
            $this->Champion->create();
            $this->Images = new ImagesController;
            $this->Images->constructClasses();
            $this->Videos = new VideosController;
            $this->Videos->constructClasses();
            $image['Image']['Image']=$this->request->data['Champion']['Image'];
            $video['Video']=array('title' => $this->request->data['Champion']['title'],
                                    'video_size' => $this->request->data['Champion']['video_size'],
                                    'url' => $this->request->data['Champion']['url'],
                                    'user_id' => $this->Auth->user('id'));
            $error=false;
            if(!empty($this->request->data['Champion']['Image']['tmp_name']) && !empty($this->request->data['Champion']['title'])){
                if(!empty($this->request->data['Champion']['url']) && !empty($this->request->data['Champion']['champion_name'])){
                    if(!empty($this->request->data['Champion']['description']) && !empty($this->request->data['Champion']['type'])){
                        $id_image=$this->Images->add($image);
                        $id_video=$this->Videos->add($video);
                    }else $error=true;
                }else $error=true;
            }else $error=true;
            if($error){
                $this->Session->setFlash('Por favor, preencha corretamente os campos de cadastro e tente novamente.','default',array('class' => 'errorMsg'));
                if(!empty($this->request->data['Champion']['Image']['tmp_name']))
                    $this->request->data['Champion']['Image']="error";
                $this->Champion->save($this->request->data);
                return;
            }
            $this->request->data['Champion']['user_id']=$this->Auth->user('id');
            $this->request->data['Champion']['id_image']=$id_image['Image']['id'];
            $this->request->data['Champion']['id_video']=$id_video['Video']['id'];
            $this->request->data['Champion']['Image']="default";
            if ($this->Champion->save($this->request->data)) {
                $this->Session->setFlash('Champion cadastrado com sucesso','default',array('class' => 'sucessMsg'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Por favor, preencha corretamente os campos de cadastro e tente novamente.','default',array('class' => 'errorMsg'));
            }
        }
    }

    /**
     * edit method
     *
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        $this->Champion->id = $id;
        if (!$this->Champion->exists()) {
            throw new NotFoundException(__('Invalid champion'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Champion->save($this->request->data)) {
                $this->Session->setFlash('Edição realizada com sucesso','default',array('class' => 'sucessMsg'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Por favor, preencha corretamente os campos de cadastro e tente novamente.','default',array('class' => 'errorMsg'));
            }
        } else {
            $this->request->data = $this->Champion->read(null, $id);
        }
    }

    /**
     * delete method
     *
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Champion->id = $id;
        if (!$this->Champion->exists()) {
            throw new NotFoundException(__('Invalid champion'));
        }
        if ($this->Champion->delete()) {
            $this->Session->setFlash('Champion excluído com sucesso','default',array('class' => 'sucessMsg'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash('Erro na exclusão do champion','default',array('class' => 'errorMsg'));
        $this->redirect(array('action' => 'index'));
    }

}

/*if($id_image=$this->Images->add($image)==false){
                    $this->Champion->save($this->request->data);
                    $this->Session->setFlash('Por favor, preencha corretamente os campos de cadastro e tente novamente.','default',array('class' => 'errorMsg'));
                    return;
                }
                if($id_video=$this->Videos->add($video)==false){
                    $this->Images->delete($id_image['Image']['id']);
                    $this->request->data['Champion']['Image']="error";
                    $this->Session->setFlash('Por favor, preencha corretamente os campos de cadastro e tente novamente.','default',array('class' => 'errorMsg'));
                    $this->Champion->save($this->request->data);
                    return;
                }*/